package cn.big.bigd.biocode.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * 发动邮件
 * @author wangfan
 *
 */
public class EmailUtils {

    private static final Logger logger = LoggerFactory.getLogger(EmailUtils.class);

    public static boolean sendHtmlMail(String subject,String content,String receiver){

        boolean result = true;

        try{
            JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();  

            // 设定mail server  
            // 设定mail server  
            senderImpl.setHost(Constant.email_host);  
            senderImpl.setPort(Constant.email_port);
            senderImpl.setUsername(Constant.email);                             // 根据自己的情况,设置发件邮箱地址  
            senderImpl.setPassword(Constant.email_password);                    // 根据自己的情况, 设置password  
            senderImpl.setDefaultEncoding("UTF-8");
            Properties prop = new Properties();  
            prop.put("mail.smtp.auth", "true");                                 // 将这个参数设为true，让服务器进行认证,认证用户名和密码是否正确  
            prop.put("mail.smtp.ssl.enable", "false");
            prop.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            senderImpl.setJavaMailProperties(prop);  

            // 建立邮件消息,发送简单邮件和html邮件的区别  
            MimeMessage mailMessage = senderImpl.createMimeMessage();  
            MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage);  

            // 设置收件人，寄件人  
            messageHelper.setTo(receiver);  
            messageHelper.setFrom(Constant.email);  
            messageHelper.setSubject(subject);
            // true 表示启动HTML格式的邮件  
            messageHelper.setText(content,true);  

            // 发送邮件  
            senderImpl.send(mailMessage);  
        } catch (MessagingException e) {
            
            result = false;
            logger.info("EmailUtils.method [sendHtmlMail]: email send result-" + result +",error message-" + e);
        }

        return result;
    }

}